package com.ruoyi.quartz.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.quartz.domain.*;
import com.ruoyi.quartz.mapper.CrmYgxxMapper;
import com.ruoyi.quartz.service.*;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class CrmServiceImpl implements ICrmService {

    private static final Logger logger = LoggerFactory.getLogger(CrmServiceImpl.class);

    // CRM
    private static final int TIMEOUT = 30000;
    private static final String MAC_NAME = "HmacSHA1";
    private static final String ENCODING = "utf-8";
    private static final String secretKey ="59746cbc-d86c-43e2-94d3-c6202dea9100";

    @Autowired
    public ICrmYgxxService ygxxService;

    @Autowired
    public ICrmKhglService khglService;

    @Autowired
    public ICrmKhExtendService khExtendService;

    @Autowired
    public ICrmKhggcService kggcService;

    @Autowired
    public ICrmCpxxService cpxxService;

    @Autowired
    public ICrmFhdService fhdService;

    @Autowired
    public ICrmFhdDetailService fhdDetailService;

    @Autowired
    public ICrmHtddService htddService;

    @Autowired
    public ICrmHtddDetailService htddDetailService;

    @Override
    public void crmCpxx() {
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "154");
        s.put("page", "1");
        s.put("pagesize", "100");
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            cpxxService.clearCpxx();
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp", System.currentTimeMillis() / 1000);
                param.put("datatype", "154");
                param.put("page", i + "");
                param.put("pagesize", "100");
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + postSign + "&key=" + secretId + "&appid=" + id + "&type=" + type;
                JSONObject res = httpPost(postUrl, postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        CrmCpxx cpxx = new CrmCpxx();
                        cpxx.setCpid(Long.parseLong(info.getString("cpid")));
                        cpxx.setCpname(info.getString("cpname"));
                        cpxx.setCptype(info.getString("cptype"));
                        cpxx.setCpno(info.getString("cpno"));
                        JSONObject typeData = info.getJSONObject("typeid");
                        cpxx.setTypeName(typeData.getString("title"));
                        cpxx.setTypeId(typeData.getString("id"));
                        cpxx.setCpunit(info.getString("cpunit"));
                        cpxx.setIsstop(info.getString("isstop"));
                        cpxx.setPreprice1(info.getBigDecimal("preprice1"));
                        cpxx.setCbprice(info.getBigDecimal("cbprice"));
                        cpxx.setPreprice2(info.getBigDecimal("preprice2"));
                        cpxx.setIsstock(info.getString("isstock"));
                        JSONObject dataUser = info.getJSONObject("data_userid");
                        cpxx.setDataUsername(dataUser.getString("title"));
                        cpxx.setDataUserid(dataUser.getString("id"));
                        cpxx.setCpguige(info.getString("guige"));
                        cpxxService.insertCrmCpxx(cpxx);
                    }
                }
            }
        }
    }

    @Override
    public void crmFhd() {
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "169");
        s.put("page", "1");
        s.put("pagesize", "100");
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            fhdService.clearFhd();
            fhdDetailService.clearFhdDetail();
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "169");
                param.put("page", i+"");
                param.put("pagesize", "100");
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp",  System.currentTimeMillis()/1000);
                        pSub.put("datatype", "169");
                        pSub.put("msgid", info.getString("fh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+subPostSign+"&key="+secretId+"&appid="+id+"&type=view";
                        JSONObject subRes = httpPost(subPostUrl,subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        CrmFhd fhd = new CrmFhd();
                        fhd.setFhId(detail.getLong("fh_id"));
                        JSONObject fhCustomer = detail.getJSONObject("fh_customerid");
                        fhd.setFhCustomerid(fhCustomer.getString("id"));
                        fhd.setFhCustomerName(fhCustomer.getString("title"));
                        fhd.setFhDate(detail.getString("fh_date"));
                        JSONObject fhPreside = detail.getJSONObject("fh_preside");
                        fhd.setDeptid(fhPreside.getString("deptid"));
                        fhd.setFhPresideId(fhPreside.getString("id"));
                        fhd.setFhPresideName(fhPreside.getString("title"));
                        fhd.setFhMode(detail.getString("fh_mode"));
                        fhd.setFhNumber(detail.getString("fh_number"));
                        fhd.setFhLxrid(detail.getString("fh_lxrid"));
                        fhd.setFhLinkman(detail.getString("fh_linkman"));
                        fhd.setFhHandset(detail.getString("fh_handset"));
                        fhd.setFhTel(detail.getString("fh_tel"));
                        fhd.setFhTitle(detail.getString("fh_title"));
                        JSONObject fhHtorder = detail.getJSONObject("fh_htorder");
                        fhd.setFhHtorderName(fhHtorder.getString("title"));
                        fhd.setHtNumber(fhHtorder.getString("number"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        fhd.setCreater(dataUser.getString("title"));
                        fhd.setFhRemark(detail.getString("fh_remark"));
                        fhd.setFhState(detail.getString("fh_state"));
                        fhd.setShenhestate(detail.getString("shenhestate"));
                        fhd.setDiyDdl1(detail.getString("diy_ddl1"));
                        fhd.setAddtime(detail.getString("addtime"));
                        fhdService.insertCrmFhd(fhd);
                        JSONArray child_mx = detail.getJSONArray("child_mx");
                        if(child_mx.size()>0){
                            for(int x=0;x<child_mx.size();x++){
                                JSONObject child = child_mx.getJSONObject(x);
                                CrmFhdDetail fhdDetail = new CrmFhdDetail();
                                fhdDetail.setFhNumber(detail.getString("fh_id"));
                                fhdDetail.setProductId(child.getString("产品ID"));
                                fhdDetail.setProductName(child.getString("产品名称"));
                                fhdDetail.setProductType(child.getString("产品型号"));
                                fhdDetail.setProductSpecs(child.getString("产品规格"));
                                fhdDetail.setUnit(child.getString("计件单位"));
                                if(child.getString("数量")!=null&&child.getString("数量").length()>0){
                                    fhdDetail.setNums(new BigDecimal(child.getString("数量")));
                                }
                                if(child.getString("零售价")!=null&&child.getString("零售价").length()>0){
                                    fhdDetail.setRetailPrice(new BigDecimal(child.getString("零售价")));
                                }
                                if(child.getString("单价")!=null&&child.getString("单价").length()>0){
                                    fhdDetail.setUnitPrice(new BigDecimal(child.getString("单价")));
                                }
                                if(child.getString("总价")!=null&&child.getString("总价").length()>0){
                                    fhdDetail.setTotalPrice(new BigDecimal(child.getString("总价")));
                                }
                                fhdDetail.setRemarks(child.getString("明细备注"));
                                fhdDetailService.insertCrmFhdDetail(fhdDetail);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void crmHtdd() {
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "150");
        s.put("page", "1");
        s.put("pagesize", "100");
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            htddService.clrearHtdd();
            htddDetailService.clearHtddDetail();
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "150");
                param.put("page", i+"");
                param.put("pagesize", "100");
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0) {
                    for (int j = 0; j < data.size(); j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp", System.currentTimeMillis() / 1000);
                        pSub.put("datatype", "150");
                        pSub.put("msgid", info.getString("ht_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + subPostSign + "&key=" + secretId + "&appid=" + id + "&type=view";
                        JSONObject subRes = httpPost(subPostUrl, subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        CrmHtdd htdd = new CrmHtdd();
                        htdd.setHtId(detail.getLong("ht_id"));
                        JSONObject dtCustomer = detail.getJSONObject("ht_customerid");
                        htdd.setHtCustomerid(dtCustomer.getString("id"));
                        htdd.setHtCustomerName(dtCustomer.getString("title"));
                        htdd.setHtDate(detail.getString("ht_date"));
                        JSONObject htPreside = detail.getJSONObject("ht_preside");
                        htdd.setDeptid(htPreside.getString("deptid"));
                        htdd.setHtPresideId(htPreside.getString("id"));
                        htdd.setHtPresideName(htPreside.getString("title"));
                        htdd.setHtOrder(detail.getString("ht_order"));
                        htdd.setHtNumber(detail.getString("ht_number"));
                        htdd.setHtState(detail.getString("ht_state"));
                        if(detail.getString("ht_hkmoney")!=null&&detail.getString("ht_hkmoney").length()>0) {
                            htdd.setHtHkmoney(new BigDecimal(detail.getString("ht_hkmoney")));
                        }
                        if(detail.getString("ht_summoney")!=null&&detail.getString("ht_summoney").length()>0) {
                            htdd.setHtSummoney(new BigDecimal(detail.getString("ht_summoney")));
                        }
                        htdd.setHtTitle(detail.getString("ht_title"));
                        htdd.setHtLxrinfo(detail.getString("ht_lxrinfo"));
                        htdd.setHtType(detail.getString("ht_type"));
                        htdd.setHtPaymode(detail.getString("ht_paymode"));
                        htdd.setHtBegindate(detail.getString("ht_begindate"));
                        htdd.setHtEnddate(detail.getString("ht_enddate"));
                        htdd.setHtMoneyzhekou(detail.getString("ht_moneyzhekou"));
                        htdd.setHtCkstate(detail.getString("ht_ckstate"));
                        htdd.setDiyDdl1(detail.getString("diy_ddl1"));
                        htdd.setHtFahuostate(detail.getString("ht_fahuostate"));
                        htdd.setShenhestate(detail.getString("shenhestate"));
                        htdd.setAddtime(detail.getString("addtime"));
                        htddService.insertCrmHtdd(htdd);
                        JSONArray child_mx = detail.getJSONArray("child_mx");
                        if(child_mx.size()>0){
                            for(int x=0;x<child_mx.size();x++){
                                JSONObject child = child_mx.getJSONObject(x);
                                CrmHtddDetail htddDetail = new CrmHtddDetail();
                                htddDetail.setHtNumber(detail.getString("ht_id"));
                                htddDetail.setProductCode(child.getString("产品ID"));
                                htddDetail.setProductName(child.getString("产品名称"));
                                htddDetail.setProductType(child.getString("产品型号"));
                                htddDetail.setProductSpecs(child.getString("产品规格"));
                                htddDetail.setUnit(child.getString("计件单位"));
                                if(child.getString("数量")!=null&&child.getString("数量").length()>0){
                                    htddDetail.setNums(new BigDecimal(child.getString("数量")));
                                }
                                if(child.getString("零售价")!=null&&child.getString("零售价").length()>0){
                                    htddDetail.setRetailPrice(new BigDecimal(child.getString("零售价")));
                                }
                                if(child.getString("单价")!=null&&child.getString("单价").length()>0){
                                    htddDetail.setUnitPrice(new BigDecimal(child.getString("单价")));
                                }
                                if(child.getString("总价")!=null&&child.getString("总价").length()>0){
                                    htddDetail.setTotalPrice(new BigDecimal(child.getString("总价")));
                                }
                                htddDetail.setRemarks(child.getString("明细备注"));
                                htddDetailService.insertCrmHtddDetail(htddDetail);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void crmKhgl() {
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "148");
        s.put("page", "1");
        s.put("pagesize", "100");
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            khglService.clearKhgl();
            khExtendService.clearKhExtentd();
            for(int i=1;i<=pages;i++){
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "148");
                param.put("page", i+"");
                param.put("pagesize", "100");
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++){
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp",  System.currentTimeMillis()/1000);
                        pSub.put("datatype", "148");
                        pSub.put("msgid", info.getString("kh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+subPostSign+"&key="+secretId+"&appid="+id+"&type=view";
                        JSONObject subRes = httpPost(subPostUrl,subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        CrmKhgl khgl = new CrmKhgl();
                        khgl.setKhId(detail.getString("kh_id"));
                        khgl.setKhClass(detail.getString("kh_class"));
                        khgl.setKhName(detail.getString("kh_name"));
                        khgl.setKhShortname(detail.getString("kh_shortname"));
                        khgl.setKhAddress(detail.getString("kh_address"));
                        khgl.setKhCountry(detail.getString("kh_country"));
                        khgl.setKhProvince(detail.getString("kh_province"));
                        khgl.setKhCity(detail.getString("kh_city"));
                        khgl.setKhCoaddress(detail.getString("kh_coaddress"));
                        khgl.setKhHottype(detail.getString("kh_hottype"));
                        khgl.setKhHotlevel(detail.getString("kh_hotlevel"));
                        khgl.setKhType(detail.getString("kh_type"));
                        khgl.setKhStatus(detail.getString("kh_status"));
                        khgl.setKhSn(detail.getString("kh_sn"));
                        khgl.setKhHandset(detail.getString("kh_handset"));
                        khgl.setKhFrom(detail.getString("kh_from"));
                        khgl.setKhCreditgrade(detail.getString("kh_creditgrade"));
                        khgl.setKhValrating(detail.getString("kh_valrating"));
                        khgl.setKhRemark(detail.getString("kh_remark"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        khgl.setDataUserid(dataUser.getString("id"));
                        khgl.setDataUserName(dataUser.getString("title"));
                        khgl.setDeptid(dataUser.getString("deptid"));
                        JSONObject yopuser = detail.getJSONObject("kh_yopuser");
                        khgl.setKhYopuserid(yopuser.getString("id"));
                        khgl.setKhYopuserName(yopuser.getString("title"));
                        khgl.setKhYopuserDeptid(yopuser.getString("deptid"));
                        khgl.setAddtime(detail.getString("addtime"));
                        khgl.setKhFenpeitime(detail.getString("kh_fenpeitime"));
                        khgl.setKhGenzongtime(detail.getString("kh_genzongtime"));
                        khgl.setKhMarkclass(detail.getString("kh_markclass"));
                        khgl.setDiyCbox1(detail.getString("diy_cbox1"));
                        khgl.setDiyCbox2(detail.getString("diy_cbox2"));
                        khgl.setDiyDdl2(detail.getString("diy_ddl2"));
                        khgl.setDiyDdl3(detail.getString("diy_ddl3"));
                        khgl.setKhIndustry(detail.getString("kh_industry"));
                        khglService.insertCrmKhgl(khgl);

                        CrmKhExtend khExtend = new CrmKhExtend();
                        khExtend.setKhId(detail.getString("kh_id"));
                        khExtend.setDiyCbox1(detail.getString("diy_cbox1"));
                        khExtend.setDiyCbox2(detail.getString("diy_cbox2"));
                        khExtend.setDiyCbox3(detail.getString("diy_cbox3"));
                        khExtend.setDiyCbox4(detail.getString("diy_cbox4"));
                        khExtend.setDiyCbox5(detail.getString("diy_cbox5"));
                        khExtend.setDiyCbox6(detail.getString("diy_cbox6"));
                        khExtend.setDiyCbox7(detail.getString("diy_cbox7"));
                        khExtend.setDiyCbox8(detail.getString("diy_cbox8"));
                        khExtend.setDiyDdl1(detail.getString("diy_ddl1"));
                        khExtend.setDiyDdl2(detail.getString("diy_ddl2"));
                        khExtend.setDiyDdl3(detail.getString("diy_ddl3"));
                        khExtend.setDiyStr1(detail.getString("diy_str1"));
                        khExtend.setDiyDdl4(detail.getString("diy_ddl4"));
                        khExtendService.insertCrmKhExtend(khExtend);
                    }
                }
            }
        }
    }

    @Override
    public void crmKhggc() {
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "238");
        s.put("page", "1");
        s.put("pagesize", "100");
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            kggcService.clearKhggc();
            for(int i=1;i<=pages;i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp", System.currentTimeMillis() / 1000);
                param.put("datatype", "238");
                param.put("page", i + "");
                param.put("pagesize", "100");
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + postSign + "&key=" + secretId + "&appid=" + id + "&type=" + type;
                JSONObject res = httpPost(postUrl, postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp", System.currentTimeMillis() / 1000);
                        pSub.put("datatype", "238");
                        pSub.put("msgid", info.getString("kh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + subPostSign + "&key=" + secretId + "&appid=" + id + "&type=view";
                        JSONObject subRes = httpPost(subPostUrl, subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        CrmKhggc khggc = new CrmKhggc();
                        khggc.setKhId(detail.getString("kh_id"));
                        khggc.setKhClass(detail.getString("kh_class"));
                        khggc.setKhName(detail.getString("kh_name"));
                        khggc.setKhSex(detail.getString("kh_sex"));
                        khggc.setKhShortname(detail.getString("kh_shortname"));
                        khggc.setKhIndustry(detail.getString("kh_industry"));
                        khggc.setKhEmployees(detail.getString("kh_employees"));
                        khggc.setKhAddress(detail.getString("kh_address"));
                        khggc.setKhCountry(detail.getString("kh_country"));
                        khggc.setKhProvince(detail.getString("kh_province"));
                        khggc.setKhCity(detail.getString("kh_city"));
                        khggc.setKhCoaddress(detail.getString("kh_coaddress"));
                        khggc.setKhHottype(detail.getString("kh_hottype"));
                        khggc.setKhHotlevel(detail.getString("kh_hotlevel"));
                        khggc.setKhHotfl(detail.getString("kh_hotfl"));
                        khggc.setKhHotmemo(detail.getString("kh_hotmemo"));
                        khggc.setKhType(detail.getString("kh_type"));
                        khggc.setKhStatus(detail.getString("kh_status"));
                        khggc.setKhSn(detail.getString("kh_sn"));
                        khggc.setKhHandset(detail.getString("kh_handset"));
                        khggc.setKhEmail(detail.getString("kh_email"));
                        khggc.setKhTel(detail.getString("kh_tel"));
                        khggc.setKhWeixin(detail.getString("kh_weixin"));
                        khggc.setKhQq(detail.getString("kh_qq"));
                        khggc.setKhSkype(detail.getString("kh_skype"));
                        khggc.setKhWangwang(detail.getString("kh_wangwang"));
                        khggc.setKhWorktel(detail.getString("kh_worktel"));
                        khggc.setKhFax(detail.getString("kh_fax"));
                        khggc.setKhPst(detail.getString("kh_pst"));
                        khggc.setKhDepartment(detail.getString("kh_department"));
                        khggc.setKhAppellation(detail.getString("kh_appellation"));
                        khggc.setKhPreside(detail.getString("kh_preside"));
                        khggc.setKhHeadship(detail.getString("kh_headship"));
                        khggc.setKhWeb(detail.getString("kh_web"));
                        khggc.setKhBefontof(detail.getString("kh_befontof"));
                        khggc.setKhFrom(detail.getString("kh_from"));
                        khggc.setKhBillinfo(detail.getString("kh_billinfo"));
                        khggc.setKhInfo(detail.getString("kh_info"));
                        khggc.setKhRalagrade(detail.getString("kh_ralagrade"));
                        khggc.setKhCreditgrade(detail.getString("kh_creditgrade"));
                        khggc.setKhValrating(detail.getString("kh_valrating"));
                        khggc.setKhContype(detail.getString("kh_contype"));
                        khggc.setKhCttype(detail.getString("kh_cttype"));
                        khggc.setKhCtnumber(detail.getString("kh_ctnumber"));
                        khggc.setKhRemark(detail.getString("kh_remark"));
                        khggc.setKhJibie(detail.getString("kh_jibie"));
                        JSONObject yopuser = detail.getJSONObject("kh_yopuser");
                        khggc.setKhYopuserId(yopuser.getString("id"));
                        khggc.setKhYopuserName(yopuser.getString("title"));
                        khggc.setAddtime(detail.getString("add_time"));
                        khggc.setKhGenzongtime(detail.getString("genzong_time"));
                        khggc.setKhMarkclass(detail.getString("kh_markclass"));
                        khggc.setKhHuishoudate(detail.getString("kh_huishoudate"));
                        khggc.setShenhestate(detail.getString("shenhestate"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        khggc.setDataUserid(dataUser.getString("id"));
                        khggc.setDataUsername(dataUser.getString("title"));
                        khggc.setDiyCbox1(detail.getString("diy_cbox1"));
                        khggc.setDiyCbox2(detail.getString("diy_cbox2"));
                        khggc.setDiyDdl2(detail.getString("diy_ddl2"));
                        khggc.setDiyDdl3(detail.getString("diy_ddl3"));
                        khggc.setDiyStr1(detail.getString("diy_str1"));
                        kggcService.insertCrmKhggc(khggc);
                    }
                }
            }
        }
    }

    @Override
    public int crmYgxxDaily() {
        String startdate = getNearWeekDate(-8)+" 00:00:00";
        String enddate = getNearWeekDate(-1) +" 23:59:59";
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "167");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            for(int i=1;i<=pages;i++){
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "167");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray staff = res.getJSONArray("data");
                if(staff.size()>0){
                    for (int j=0;j<staff.size();j++){
                        JSONObject info = staff.getJSONObject(j).getJSONObject("detail");
                        ygxxService.deleteCrmYgxxByEmpid(info.getString("empid"));
                        CrmYgxx ygxx = new CrmYgxx();
                        ygxx.setEmpid(info.getString("empid"));
                        ygxx.setEmpName(info.getString("emp_name"));
                        ygxx.setEmpNo(info.getString("emp_no"));
                        JSONObject deptInfo = info.getJSONObject("deptid");
                        ygxx.setDeptid(deptInfo.getString("id"));
                        ygxx.setDeptName(deptInfo.getString("title"));
                        ygxx.setDutyid(info.getString("dutyid"));
                        ygxx.setEmpZaizhi(info.getString("emp_zaizhi"));
                        ygxxService.insertCrmYgxx(ygxx);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmYgxxHourly() {
        String date = getCurrentDate();
        String startdate = date+" 00:00:00";
        String enddate = date+" 23:59:59";
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "167");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            for(int i=1;i<=pages;i++){
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "167");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray staff = res.getJSONArray("data");
                if(staff.size()>0){
                    for (int j=0;j<staff.size();j++){
                        JSONObject info = staff.getJSONObject(j).getJSONObject("detail");
                        ygxxService.deleteCrmYgxxByEmpid(info.getString("empid"));
                        CrmYgxx ygxx = new CrmYgxx();
                        ygxx.setEmpid(info.getString("empid"));
                        ygxx.setEmpName(info.getString("emp_name"));
                        ygxx.setEmpNo(info.getString("emp_no"));
                        JSONObject deptInfo = info.getJSONObject("deptid");
                        ygxx.setDeptid(deptInfo.getString("id"));
                        ygxx.setDeptName(deptInfo.getString("title"));
                        ygxx.setDutyid(info.getString("dutyid"));
                        ygxx.setEmpZaizhi(info.getString("emp_zaizhi"));
                        ygxxService.insertCrmYgxx(ygxx);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmKhglDaily() {
        String startdate = getNearWeekDate(-8)+" 00:00:00";
        String enddate = getNearWeekDate(-1) +" 23:59:59";
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "148");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign = appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            for(int i=1;i<=pages;i++){
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "148");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++){
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp",  System.currentTimeMillis()/1000);
                        pSub.put("datatype", "148");
                        pSub.put("msgid", info.getString("kh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+subPostSign+"&key="+secretId+"&appid="+id+"&type=view";
                        JSONObject subRes = httpPost(subPostUrl,subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        khglService.deleteCrmKhglByKhId(detail.getString("kh_id"));
                        khExtendService.deleteCrmKhExtendByKhId(detail.getString("kh_id"));
                        CrmKhgl khgl = new CrmKhgl();
                        khgl.setKhId(detail.getString("kh_id"));
                        khgl.setKhClass(detail.getString("kh_class"));
                        khgl.setKhName(detail.getString("kh_name"));
                        khgl.setKhShortname(detail.getString("kh_shortname"));
                        khgl.setKhAddress(detail.getString("kh_address"));
                        khgl.setKhCountry(detail.getString("kh_country"));
                        khgl.setKhProvince(detail.getString("kh_province"));
                        khgl.setKhCity(detail.getString("kh_city"));
                        khgl.setKhCoaddress(detail.getString("kh_coaddress"));
                        khgl.setKhHottype(detail.getString("kh_hottype"));
                        khgl.setKhHotlevel(detail.getString("kh_hotlevel"));
                        khgl.setKhType(detail.getString("kh_type"));
                        khgl.setKhStatus(detail.getString("kh_status"));
                        khgl.setKhSn(detail.getString("kh_sn"));
                        khgl.setKhHandset(detail.getString("kh_handset"));
                        khgl.setKhFrom(detail.getString("kh_from"));
                        khgl.setKhCreditgrade(detail.getString("kh_creditgrade"));
                        khgl.setKhValrating(detail.getString("kh_valrating"));
                        khgl.setKhRemark(detail.getString("kh_remark"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        khgl.setDataUserid(dataUser.getString("id"));
                        khgl.setDataUserName(dataUser.getString("title"));
                        khgl.setDeptid(dataUser.getString("deptid"));
                        JSONObject yopuser = detail.getJSONObject("kh_yopuser");
                        khgl.setKhYopuserid(yopuser.getString("id"));
                        khgl.setKhYopuserName(yopuser.getString("title"));
                        khgl.setKhYopuserDeptid(yopuser.getString("deptid"));
                        khgl.setAddtime(detail.getString("addtime"));
                        khgl.setKhFenpeitime(detail.getString("kh_fenpeitime"));
                        khgl.setKhGenzongtime(detail.getString("kh_genzongtime"));
                        khgl.setKhMarkclass(detail.getString("kh_markclass"));
                        khgl.setDiyCbox1(detail.getString("diy_cbox1"));
                        khgl.setDiyCbox2(detail.getString("diy_cbox2"));
                        khgl.setDiyDdl2(detail.getString("diy_ddl2"));
                        khgl.setDiyDdl3(detail.getString("diy_ddl3"));
                        khgl.setKhIndustry(detail.getString("kh_industry"));
                        khglService.insertCrmKhgl(khgl);

                        CrmKhExtend khExtend = new CrmKhExtend();
                        khExtend.setKhId(detail.getString("kh_id"));
                        khExtend.setDiyCbox1(detail.getString("diy_cbox1"));
                        khExtend.setDiyCbox2(detail.getString("diy_cbox2"));
                        khExtend.setDiyCbox3(detail.getString("diy_cbox3"));
                        khExtend.setDiyCbox4(detail.getString("diy_cbox4"));
                        khExtend.setDiyCbox5(detail.getString("diy_cbox5"));
                        khExtend.setDiyCbox6(detail.getString("diy_cbox6"));
                        khExtend.setDiyCbox7(detail.getString("diy_cbox7"));
                        khExtend.setDiyCbox8(detail.getString("diy_cbox8"));
                        khExtend.setDiyDdl1(detail.getString("diy_ddl1"));
                        khExtend.setDiyDdl2(detail.getString("diy_ddl2"));
                        khExtend.setDiyDdl3(detail.getString("diy_ddl3"));
                        khExtend.setDiyStr1(detail.getString("diy_str1"));
                        khExtend.setDiyDdl4(detail.getString("diy_ddl4"));
                        khExtendService.insertCrmKhExtend(khExtend);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmKhglHourly() {
        String date = getCurrentDate();
        String startdate = date+" 00:00:00";
        String enddate = date+" 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "148");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign = appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            for(int i=1;i<=pages;i++){
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "148");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++){
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp",  System.currentTimeMillis()/1000);
                        pSub.put("datatype", "148");
                        pSub.put("msgid", info.getString("kh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+subPostSign+"&key="+secretId+"&appid="+id+"&type=view";
                        JSONObject subRes = httpPost(subPostUrl,subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        khglService.deleteCrmKhglByKhId(detail.getString("kh_id"));
                        khExtendService.deleteCrmKhExtendByKhId(detail.getString("kh_id"));
                        CrmKhgl khgl = new CrmKhgl();
                        khgl.setKhId(detail.getString("kh_id"));
                        khgl.setKhClass(detail.getString("kh_class"));
                        khgl.setKhName(detail.getString("kh_name"));
                        khgl.setKhShortname(detail.getString("kh_shortname"));
                        khgl.setKhAddress(detail.getString("kh_address"));
                        khgl.setKhCountry(detail.getString("kh_country"));
                        khgl.setKhProvince(detail.getString("kh_province"));
                        khgl.setKhCity(detail.getString("kh_city"));
                        khgl.setKhCoaddress(detail.getString("kh_coaddress"));
                        khgl.setKhHottype(detail.getString("kh_hottype"));
                        khgl.setKhHotlevel(detail.getString("kh_hotlevel"));
                        khgl.setKhType(detail.getString("kh_type"));
                        khgl.setKhStatus(detail.getString("kh_status"));
                        khgl.setKhSn(detail.getString("kh_sn"));
                        khgl.setKhHandset(detail.getString("kh_handset"));
                        khgl.setKhFrom(detail.getString("kh_from"));
                        khgl.setKhCreditgrade(detail.getString("kh_creditgrade"));
                        khgl.setKhValrating(detail.getString("kh_valrating"));
                        khgl.setKhRemark(detail.getString("kh_remark"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        khgl.setDataUserid(dataUser.getString("id"));
                        khgl.setDataUserName(dataUser.getString("title"));
                        khgl.setDeptid(dataUser.getString("deptid"));
                        JSONObject yopuser = detail.getJSONObject("kh_yopuser");
                        khgl.setKhYopuserid(yopuser.getString("id"));
                        khgl.setKhYopuserName(yopuser.getString("title"));
                        khgl.setKhYopuserDeptid(yopuser.getString("deptid"));
                        khgl.setAddtime(detail.getString("addtime"));
                        khgl.setKhFenpeitime(detail.getString("kh_fenpeitime"));
                        khgl.setKhGenzongtime(detail.getString("kh_genzongtime"));
                        khgl.setKhMarkclass(detail.getString("kh_markclass"));
                        khgl.setDiyCbox1(detail.getString("diy_cbox1"));
                        khgl.setDiyCbox2(detail.getString("diy_cbox2"));
                        khgl.setDiyDdl2(detail.getString("diy_ddl2"));
                        khgl.setDiyDdl3(detail.getString("diy_ddl3"));
                        khgl.setKhIndustry(detail.getString("kh_industry"));
                        khglService.insertCrmKhgl(khgl);

                        CrmKhExtend khExtend = new CrmKhExtend();
                        khExtend.setKhId(detail.getString("kh_id"));
                        khExtend.setDiyCbox1(detail.getString("diy_cbox1"));
                        khExtend.setDiyCbox2(detail.getString("diy_cbox2"));
                        khExtend.setDiyCbox3(detail.getString("diy_cbox3"));
                        khExtend.setDiyCbox4(detail.getString("diy_cbox4"));
                        khExtend.setDiyCbox5(detail.getString("diy_cbox5"));
                        khExtend.setDiyCbox6(detail.getString("diy_cbox6"));
                        khExtend.setDiyCbox7(detail.getString("diy_cbox7"));
                        khExtend.setDiyCbox8(detail.getString("diy_cbox8"));
                        khExtend.setDiyDdl1(detail.getString("diy_ddl1"));
                        khExtend.setDiyDdl2(detail.getString("diy_ddl2"));
                        khExtend.setDiyDdl3(detail.getString("diy_ddl3"));
                        khExtend.setDiyStr1(detail.getString("diy_str1"));
                        khExtend.setDiyDdl4(detail.getString("diy_ddl4"));
                        khExtendService.insertCrmKhExtend(khExtend);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmKhggcDaily() {
        String startdate = getNearWeekDate(-8)+" 00:00:00";
        String enddate = getNearWeekDate(-1) +" 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "238");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            for(int i=1;i<=pages;i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp", System.currentTimeMillis() / 1000);
                param.put("datatype", "238");
                param.put("page", i + "");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + postSign + "&key=" + secretId + "&appid=" + id + "&type=" + type;
                JSONObject res = httpPost(postUrl, postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp", System.currentTimeMillis() / 1000);
                        pSub.put("datatype", "238");
                        pSub.put("msgid", info.getString("kh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + subPostSign + "&key=" + secretId + "&appid=" + id + "&type=view";
                        JSONObject subRes = httpPost(subPostUrl, subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        kggcService.deleteCrmKhggcByKhId(detail.getString("kh_id"));
                        CrmKhggc khggc = new CrmKhggc();
                        khggc.setKhId(detail.getString("kh_id"));
                        khggc.setKhClass(detail.getString("kh_class"));
                        khggc.setKhName(detail.getString("kh_name"));
                        khggc.setKhSex(detail.getString("kh_sex"));
                        khggc.setKhShortname(detail.getString("kh_shortname"));
                        khggc.setKhIndustry(detail.getString("kh_industry"));
                        khggc.setKhEmployees(detail.getString("kh_employees"));
                        khggc.setKhAddress(detail.getString("kh_address"));
                        khggc.setKhCountry(detail.getString("kh_country"));
                        khggc.setKhProvince(detail.getString("kh_province"));
                        khggc.setKhCity(detail.getString("kh_city"));
                        khggc.setKhCoaddress(detail.getString("kh_coaddress"));
                        khggc.setKhHottype(detail.getString("kh_hottype"));
                        khggc.setKhHotlevel(detail.getString("kh_hotlevel"));
                        khggc.setKhHotfl(detail.getString("kh_hotfl"));
                        khggc.setKhHotmemo(detail.getString("kh_hotmemo"));
                        khggc.setKhType(detail.getString("kh_type"));
                        khggc.setKhStatus(detail.getString("kh_status"));
                        khggc.setKhSn(detail.getString("kh_sn"));
                        khggc.setKhHandset(detail.getString("kh_handset"));
                        khggc.setKhEmail(detail.getString("kh_email"));
                        khggc.setKhTel(detail.getString("kh_tel"));
                        khggc.setKhWeixin(detail.getString("kh_weixin"));
                        khggc.setKhQq(detail.getString("kh_qq"));
                        khggc.setKhSkype(detail.getString("kh_skype"));
                        khggc.setKhWangwang(detail.getString("kh_wangwang"));
                        khggc.setKhWorktel(detail.getString("kh_worktel"));
                        khggc.setKhFax(detail.getString("kh_fax"));
                        khggc.setKhPst(detail.getString("kh_pst"));
                        khggc.setKhDepartment(detail.getString("kh_department"));
                        khggc.setKhAppellation(detail.getString("kh_appellation"));
                        khggc.setKhPreside(detail.getString("kh_preside"));
                        khggc.setKhHeadship(detail.getString("kh_headship"));
                        khggc.setKhWeb(detail.getString("kh_web"));
                        khggc.setKhBefontof(detail.getString("kh_befontof"));
                        khggc.setKhFrom(detail.getString("kh_from"));
                        khggc.setKhBillinfo(detail.getString("kh_billinfo"));
                        khggc.setKhInfo(detail.getString("kh_info"));
                        khggc.setKhRalagrade(detail.getString("kh_ralagrade"));
                        khggc.setKhCreditgrade(detail.getString("kh_creditgrade"));
                        khggc.setKhValrating(detail.getString("kh_valrating"));
                        khggc.setKhContype(detail.getString("kh_contype"));
                        khggc.setKhCttype(detail.getString("kh_cttype"));
                        khggc.setKhCtnumber(detail.getString("kh_ctnumber"));
                        khggc.setKhRemark(detail.getString("kh_remark"));
                        khggc.setKhJibie(detail.getString("kh_jibie"));
                        JSONObject yopuser = detail.getJSONObject("kh_yopuser");
                        khggc.setKhYopuserId(yopuser.getString("id"));
                        khggc.setKhYopuserName(yopuser.getString("title"));
                        khggc.setAddtime(detail.getString("add_time"));
                        khggc.setKhGenzongtime(detail.getString("genzong_time"));
                        khggc.setKhMarkclass(detail.getString("kh_markclass"));
                        khggc.setKhHuishoudate(detail.getString("kh_huishoudate"));
                        khggc.setShenhestate(detail.getString("shenhestate"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        khggc.setDataUserid(dataUser.getString("id"));
                        khggc.setDataUsername(dataUser.getString("title"));
                        khggc.setDiyCbox1(detail.getString("diy_cbox1"));
                        khggc.setDiyCbox2(detail.getString("diy_cbox2"));
                        khggc.setDiyDdl2(detail.getString("diy_ddl2"));
                        khggc.setDiyDdl3(detail.getString("diy_ddl3"));
                        khggc.setDiyStr1(detail.getString("diy_str1"));
                        kggcService.insertCrmKhggc(khggc);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmKhggcHourly() {
        String date = getCurrentDate();
        String startdate = date+" 00:00:00";
        String enddate = date+" 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "238");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            for(int i=1;i<=pages;i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp", System.currentTimeMillis() / 1000);
                param.put("datatype", "238");
                param.put("page", i + "");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + postSign + "&key=" + secretId + "&appid=" + id + "&type=" + type;
                JSONObject res = httpPost(postUrl, postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp", System.currentTimeMillis() / 1000);
                        pSub.put("datatype", "238");
                        pSub.put("msgid", info.getString("kh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + subPostSign + "&key=" + secretId + "&appid=" + id + "&type=view";
                        JSONObject subRes = httpPost(subPostUrl, subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        kggcService.deleteCrmKhggcByKhId(detail.getString("kh_id"));
                        CrmKhggc khggc = new CrmKhggc();
                        khggc.setKhId(detail.getString("kh_id"));
                        khggc.setKhClass(detail.getString("kh_class"));
                        khggc.setKhName(detail.getString("kh_name"));
                        khggc.setKhSex(detail.getString("kh_sex"));
                        khggc.setKhShortname(detail.getString("kh_shortname"));
                        khggc.setKhIndustry(detail.getString("kh_industry"));
                        khggc.setKhEmployees(detail.getString("kh_employees"));
                        khggc.setKhAddress(detail.getString("kh_address"));
                        khggc.setKhCountry(detail.getString("kh_country"));
                        khggc.setKhProvince(detail.getString("kh_province"));
                        khggc.setKhCity(detail.getString("kh_city"));
                        khggc.setKhCoaddress(detail.getString("kh_coaddress"));
                        khggc.setKhHottype(detail.getString("kh_hottype"));
                        khggc.setKhHotlevel(detail.getString("kh_hotlevel"));
                        khggc.setKhHotfl(detail.getString("kh_hotfl"));
                        khggc.setKhHotmemo(detail.getString("kh_hotmemo"));
                        khggc.setKhType(detail.getString("kh_type"));
                        khggc.setKhStatus(detail.getString("kh_status"));
                        khggc.setKhSn(detail.getString("kh_sn"));
                        khggc.setKhHandset(detail.getString("kh_handset"));
                        khggc.setKhEmail(detail.getString("kh_email"));
                        khggc.setKhTel(detail.getString("kh_tel"));
                        khggc.setKhWeixin(detail.getString("kh_weixin"));
                        khggc.setKhQq(detail.getString("kh_qq"));
                        khggc.setKhSkype(detail.getString("kh_skype"));
                        khggc.setKhWangwang(detail.getString("kh_wangwang"));
                        khggc.setKhWorktel(detail.getString("kh_worktel"));
                        khggc.setKhFax(detail.getString("kh_fax"));
                        khggc.setKhPst(detail.getString("kh_pst"));
                        khggc.setKhDepartment(detail.getString("kh_department"));
                        khggc.setKhAppellation(detail.getString("kh_appellation"));
                        khggc.setKhPreside(detail.getString("kh_preside"));
                        khggc.setKhHeadship(detail.getString("kh_headship"));
                        khggc.setKhWeb(detail.getString("kh_web"));
                        khggc.setKhBefontof(detail.getString("kh_befontof"));
                        khggc.setKhFrom(detail.getString("kh_from"));
                        khggc.setKhBillinfo(detail.getString("kh_billinfo"));
                        khggc.setKhInfo(detail.getString("kh_info"));
                        khggc.setKhRalagrade(detail.getString("kh_ralagrade"));
                        khggc.setKhCreditgrade(detail.getString("kh_creditgrade"));
                        khggc.setKhValrating(detail.getString("kh_valrating"));
                        khggc.setKhContype(detail.getString("kh_contype"));
                        khggc.setKhCttype(detail.getString("kh_cttype"));
                        khggc.setKhCtnumber(detail.getString("kh_ctnumber"));
                        khggc.setKhRemark(detail.getString("kh_remark"));
                        khggc.setKhJibie(detail.getString("kh_jibie"));
                        JSONObject yopuser = detail.getJSONObject("kh_yopuser");
                        khggc.setKhYopuserId(yopuser.getString("id"));
                        khggc.setKhYopuserName(yopuser.getString("title"));
                        khggc.setAddtime(detail.getString("add_time"));
                        khggc.setKhGenzongtime(detail.getString("genzong_time"));
                        khggc.setKhMarkclass(detail.getString("kh_markclass"));
                        khggc.setKhHuishoudate(detail.getString("kh_huishoudate"));
                        khggc.setShenhestate(detail.getString("shenhestate"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        khggc.setDataUserid(dataUser.getString("id"));
                        khggc.setDataUsername(dataUser.getString("title"));
                        khggc.setDiyCbox1(detail.getString("diy_cbox1"));
                        khggc.setDiyCbox2(detail.getString("diy_cbox2"));
                        khggc.setDiyDdl2(detail.getString("diy_ddl2"));
                        khggc.setDiyDdl3(detail.getString("diy_ddl3"));
                        khggc.setDiyStr1(detail.getString("diy_str1"));
                        kggcService.insertCrmKhggc(khggc);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmCpxxDaily() {
        String startdate = getNearWeekDate(-8)+" 00:00:00";
        String enddate = getNearWeekDate(-1) +" 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "154");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp", System.currentTimeMillis() / 1000);
                param.put("datatype", "154");
                param.put("page", i + "");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + postSign + "&key=" + secretId + "&appid=" + id + "&type=" + type;
                JSONObject res = httpPost(postUrl, postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        cpxxService.deleteCrmCpxxByCpid(Long.parseLong(info.getString("cpid")));
                        CrmCpxx cpxx = new CrmCpxx();
                        cpxx.setCpid(Long.parseLong(info.getString("cpid")));
                        cpxx.setCpname(info.getString("cpname"));
                        cpxx.setCptype(info.getString("cptype"));
                        cpxx.setCpno(info.getString("cpno"));
                        JSONObject typeData = info.getJSONObject("typeid");
                        cpxx.setTypeName(typeData.getString("title"));
                        cpxx.setTypeId(typeData.getString("id"));
                        cpxx.setCpunit(info.getString("cpunit"));
                        cpxx.setIsstop(info.getString("isstop"));
                        cpxx.setPreprice1(info.getBigDecimal("preprice1"));
                        cpxx.setCbprice(info.getBigDecimal("cbprice"));
                        cpxx.setPreprice2(info.getBigDecimal("preprice2"));
                        cpxx.setIsstock(info.getString("isstock"));
                        JSONObject dataUser = info.getJSONObject("data_userid");
                        cpxx.setDataUsername(dataUser.getString("title"));
                        cpxx.setDataUserid(dataUser.getString("id"));
                        cpxx.setCpguige(info.getString("guige"));
                        cpxxService.insertCrmCpxx(cpxx);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmCpxxHourly() {
        String date = getCurrentDate();
        String startdate = date + " 00:00:00";
        String enddate = date + " 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "154");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp", System.currentTimeMillis() / 1000);
                param.put("datatype", "154");
                param.put("page", i + "");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + postSign + "&key=" + secretId + "&appid=" + id + "&type=" + type;
                JSONObject res = httpPost(postUrl, postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        cpxxService.deleteCrmCpxxByCpid(Long.parseLong(info.getString("cpid")));
                        CrmCpxx cpxx = new CrmCpxx();
                        cpxx.setCpid(Long.parseLong(info.getString("cpid")));
                        cpxx.setCpname(info.getString("cpname"));
                        cpxx.setCptype(info.getString("cptype"));
                        cpxx.setCpno(info.getString("cpno"));
                        JSONObject typeData = info.getJSONObject("typeid");
                        cpxx.setTypeName(typeData.getString("title"));
                        cpxx.setTypeId(typeData.getString("id"));
                        cpxx.setCpunit(info.getString("cpunit"));
                        cpxx.setIsstop(info.getString("isstop"));
                        cpxx.setPreprice1(info.getBigDecimal("preprice1"));
                        cpxx.setCbprice(info.getBigDecimal("cbprice"));
                        cpxx.setPreprice2(info.getBigDecimal("preprice2"));
                        cpxx.setIsstock(info.getString("isstock"));
                        JSONObject dataUser = info.getJSONObject("data_userid");
                        cpxx.setDataUsername(dataUser.getString("title"));
                        cpxx.setDataUserid(dataUser.getString("id"));
                        cpxx.setCpguige(info.getString("guige"));
                        cpxxService.insertCrmCpxx(cpxx);
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmHtddDaily() {
        String startdate = getNearWeekDate(-8)+" 00:00:00";
        String enddate = getNearWeekDate(-1) +" 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "150");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "150");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0) {
                    for (int j = 0; j < data.size(); j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp", System.currentTimeMillis() / 1000);
                        pSub.put("datatype", "150");
                        pSub.put("msgid", info.getString("ht_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + subPostSign + "&key=" + secretId + "&appid=" + id + "&type=view";
                        JSONObject subRes = httpPost(subPostUrl, subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        htddService.deleteCrmHtddByHtId(detail.getLong("ht_id"));
                        htddDetailService.deleteCrmHtddDetailByHtNumber(detail.getString("ht_id"));
                        CrmHtdd htdd = new CrmHtdd();
                        htdd.setHtId(detail.getLong("ht_id"));
                        JSONObject dtCustomer = detail.getJSONObject("ht_customerid");
                        htdd.setHtCustomerid(dtCustomer.getString("id"));
                        htdd.setHtCustomerName(dtCustomer.getString("title"));
                        htdd.setHtDate(detail.getString("ht_date"));
                        JSONObject htPreside = detail.getJSONObject("ht_preside");
                        htdd.setDeptid(htPreside.getString("deptid"));
                        htdd.setHtPresideId(htPreside.getString("id"));
                        htdd.setHtPresideName(htPreside.getString("title"));
                        htdd.setHtOrder(detail.getString("ht_order"));
                        htdd.setHtNumber(detail.getString("ht_number"));
                        htdd.setHtState(detail.getString("ht_state"));
                        if(detail.getString("ht_hkmoney")!=null&&detail.getString("ht_hkmoney").length()>0) {
                            htdd.setHtHkmoney(new BigDecimal(detail.getString("ht_hkmoney")));
                        }
                        if(detail.getString("ht_summoney")!=null&&detail.getString("ht_summoney").length()>0) {
                            htdd.setHtSummoney(new BigDecimal(detail.getString("ht_summoney")));
                        }
                        htdd.setHtTitle(detail.getString("ht_title"));
                        htdd.setHtLxrinfo(detail.getString("ht_lxrinfo"));
                        htdd.setHtType(detail.getString("ht_type"));
                        htdd.setHtPaymode(detail.getString("ht_paymode"));
                        htdd.setHtBegindate(detail.getString("ht_begindate"));
                        htdd.setHtEnddate(detail.getString("ht_enddate"));
                        htdd.setHtMoneyzhekou(detail.getString("ht_moneyzhekou"));
                        htdd.setHtCkstate(detail.getString("ht_ckstate"));
                        htdd.setDiyDdl1(detail.getString("diy_ddl1"));
                        htdd.setHtFahuostate(detail.getString("ht_fahuostate"));
                        htdd.setShenhestate(detail.getString("shenhestate"));
                        htdd.setAddtime(detail.getString("addtime"));
                        htddService.insertCrmHtdd(htdd);
                        JSONArray child_mx = detail.getJSONArray("child_mx");
                        if(child_mx.size()>0){
                            for(int x=0;x<child_mx.size();x++){
                                JSONObject child = child_mx.getJSONObject(x);
                                CrmHtddDetail htddDetail = new CrmHtddDetail();
                                htddDetail.setHtNumber(detail.getString("ht_id"));
                                htddDetail.setProductCode(child.getString("产品ID"));
                                htddDetail.setProductName(child.getString("产品名称"));
                                htddDetail.setProductType(child.getString("产品型号"));
                                htddDetail.setProductSpecs(child.getString("产品规格"));
                                htddDetail.setUnit(child.getString("计件单位"));
                                if(child.getString("数量")!=null&&child.getString("数量").length()>0){
                                    htddDetail.setNums(new BigDecimal(child.getString("数量")));
                                }
                                if(child.getString("零售价")!=null&&child.getString("零售价").length()>0){
                                    htddDetail.setRetailPrice(new BigDecimal(child.getString("零售价")));
                                }
                                if(child.getString("单价")!=null&&child.getString("单价").length()>0){
                                    htddDetail.setUnitPrice(new BigDecimal(child.getString("单价")));
                                }
                                if(child.getString("总价")!=null&&child.getString("总价").length()>0){
                                    htddDetail.setTotalPrice(new BigDecimal(child.getString("总价")));
                                }
                                htddDetail.setRemarks(child.getString("明细备注"));
                                htddDetailService.insertCrmHtddDetail(htddDetail);
                            }
                        }
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmHtddHourly() {
        String date = getCurrentDate();
        String startdate = date + " 00:00:00";
        String enddate = date + " 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "150");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "150");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0) {
                    for (int j = 0; j < data.size(); j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp", System.currentTimeMillis() / 1000);
                        pSub.put("datatype", "150");
                        pSub.put("msgid", info.getString("ht_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign=" + subPostSign + "&key=" + secretId + "&appid=" + id + "&type=view";
                        JSONObject subRes = httpPost(subPostUrl, subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        htddService.deleteCrmHtddByHtId(detail.getLong("ht_id"));
                        htddDetailService.deleteCrmHtddDetailByHtNumber(detail.getString("ht_id"));
                        CrmHtdd htdd = new CrmHtdd();
                        htdd.setHtId(detail.getLong("ht_id"));
                        JSONObject dtCustomer = detail.getJSONObject("ht_customerid");
                        htdd.setHtCustomerid(dtCustomer.getString("id"));
                        htdd.setHtCustomerName(dtCustomer.getString("title"));
                        htdd.setHtDate(detail.getString("ht_date"));
                        JSONObject htPreside = detail.getJSONObject("ht_preside");
                        htdd.setDeptid(htPreside.getString("deptid"));
                        htdd.setHtPresideId(htPreside.getString("id"));
                        htdd.setHtPresideName(htPreside.getString("title"));
                        htdd.setHtOrder(detail.getString("ht_order"));
                        htdd.setHtNumber(detail.getString("ht_number"));
                        htdd.setHtState(detail.getString("ht_state"));
                        if(detail.getString("ht_hkmoney")!=null&&detail.getString("ht_hkmoney").length()>0) {
                            htdd.setHtHkmoney(new BigDecimal(detail.getString("ht_hkmoney")));
                        }
                        if(detail.getString("ht_summoney")!=null&&detail.getString("ht_summoney").length()>0) {
                            htdd.setHtSummoney(new BigDecimal(detail.getString("ht_summoney")));
                        }
                        htdd.setHtTitle(detail.getString("ht_title"));
                        htdd.setHtLxrinfo(detail.getString("ht_lxrinfo"));
                        htdd.setHtType(detail.getString("ht_type"));
                        htdd.setHtPaymode(detail.getString("ht_paymode"));
                        htdd.setHtBegindate(detail.getString("ht_begindate"));
                        htdd.setHtEnddate(detail.getString("ht_enddate"));
                        htdd.setHtMoneyzhekou(detail.getString("ht_moneyzhekou"));
                        htdd.setHtCkstate(detail.getString("ht_ckstate"));
                        htdd.setDiyDdl1(detail.getString("diy_ddl1"));
                        htdd.setHtFahuostate(detail.getString("ht_fahuostate"));
                        htdd.setShenhestate(detail.getString("shenhestate"));
                        htdd.setAddtime(detail.getString("addtime"));
                        htddService.insertCrmHtdd(htdd);
                        JSONArray child_mx = detail.getJSONArray("child_mx");
                        if(child_mx.size()>0){
                            for(int x=0;x<child_mx.size();x++){
                                JSONObject child = child_mx.getJSONObject(x);
                                CrmHtddDetail htddDetail = new CrmHtddDetail();
                                htddDetail.setHtNumber(detail.getString("ht_id"));
                                htddDetail.setProductCode(child.getString("产品ID"));
                                htddDetail.setProductName(child.getString("产品名称"));
                                htddDetail.setProductType(child.getString("产品型号"));
                                htddDetail.setProductSpecs(child.getString("产品规格"));
                                htddDetail.setUnit(child.getString("计件单位"));
                                if(child.getString("数量")!=null&&child.getString("数量").length()>0){
                                    htddDetail.setNums(new BigDecimal(child.getString("数量")));
                                }
                                if(child.getString("零售价")!=null&&child.getString("零售价").length()>0){
                                    htddDetail.setRetailPrice(new BigDecimal(child.getString("零售价")));
                                }
                                if(child.getString("单价")!=null&&child.getString("单价").length()>0){
                                    htddDetail.setUnitPrice(new BigDecimal(child.getString("单价")));
                                }
                                if(child.getString("总价")!=null&&child.getString("总价").length()>0){
                                    htddDetail.setTotalPrice(new BigDecimal(child.getString("总价")));
                                }
                                htddDetail.setRemarks(child.getString("明细备注"));
                                htddDetailService.insertCrmHtddDetail(htddDetail);
                            }
                        }
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmFhdDaily() {
        String startdate = getNearWeekDate(-8)+" 00:00:00";
        String enddate = getNearWeekDate(-1) +" 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "169");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "169");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp",  System.currentTimeMillis()/1000);
                        pSub.put("datatype", "169");
                        pSub.put("msgid", info.getString("fh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+subPostSign+"&key="+secretId+"&appid="+id+"&type=view";
                        JSONObject subRes = httpPost(subPostUrl,subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        fhdService.deleteCrmFhdByFhId(detail.getLong("fh_id"));
                        fhdDetailService.deleteCrmFhdDetailByFhNumber(detail.getString("fh_id"));
                        CrmFhd fhd = new CrmFhd();
                        fhd.setFhId(detail.getLong("fh_id"));
                        JSONObject fhCustomer = detail.getJSONObject("fh_customerid");
                        fhd.setFhCustomerid(fhCustomer.getString("id"));
                        fhd.setFhCustomerName(fhCustomer.getString("title"));
                        fhd.setFhDate(detail.getString("fh_date"));
                        JSONObject fhPreside = detail.getJSONObject("fh_preside");
                        fhd.setDeptid(fhPreside.getString("deptid"));
                        fhd.setFhPresideId(fhPreside.getString("id"));
                        fhd.setFhPresideName(fhPreside.getString("title"));
                        fhd.setFhMode(detail.getString("fh_mode"));
                        fhd.setFhNumber(detail.getString("fh_number"));
                        fhd.setFhLxrid(detail.getString("fh_lxrid"));
                        fhd.setFhLinkman(detail.getString("fh_linkman"));
                        fhd.setFhHandset(detail.getString("fh_handset"));
                        fhd.setFhTel(detail.getString("fh_tel"));
                        fhd.setFhTitle(detail.getString("fh_title"));
                        JSONObject fhHtorder = detail.getJSONObject("fh_htorder");
                        fhd.setFhHtorderName(fhHtorder.getString("title"));
                        fhd.setHtNumber(fhHtorder.getString("number"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        fhd.setCreater(dataUser.getString("title"));
                        fhd.setFhRemark(detail.getString("fh_remark"));
                        fhd.setFhState(detail.getString("fh_state"));
                        fhd.setShenhestate(detail.getString("shenhestate"));
                        fhd.setDiyDdl1(detail.getString("diy_ddl1"));
                        fhd.setAddtime(detail.getString("addtime"));
                        fhdService.insertCrmFhd(fhd);
                        JSONArray child_mx = detail.getJSONArray("child_mx");
                        if(child_mx.size()>0){
                            for(int x=0;x<child_mx.size();x++){
                                JSONObject child = child_mx.getJSONObject(x);
                                CrmFhdDetail fhdDetail = new CrmFhdDetail();
                                fhdDetail.setFhNumber(detail.getString("fh_id"));
                                fhdDetail.setProductId(child.getString("产品ID"));
                                fhdDetail.setProductName(child.getString("产品名称"));
                                fhdDetail.setProductType(child.getString("产品型号"));
                                fhdDetail.setProductSpecs(child.getString("产品规格"));
                                fhdDetail.setUnit(child.getString("计件单位"));
                                if(child.getString("数量")!=null&&child.getString("数量").length()>0){
                                    fhdDetail.setNums(new BigDecimal(child.getString("数量")));
                                }
                                if(child.getString("零售价")!=null&&child.getString("零售价").length()>0){
                                    fhdDetail.setRetailPrice(new BigDecimal(child.getString("零售价")));
                                }
                                if(child.getString("单价")!=null&&child.getString("单价").length()>0){
                                    fhdDetail.setUnitPrice(new BigDecimal(child.getString("单价")));
                                }
                                if(child.getString("总价")!=null&&child.getString("总价").length()>0){
                                    fhdDetail.setTotalPrice(new BigDecimal(child.getString("总价")));
                                }
                                fhdDetail.setRemarks(child.getString("明细备注"));
                                fhdDetailService.insertCrmFhdDetail(fhdDetail);
                            }
                        }
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public int crmFhdHourly() {
        String date = getCurrentDate();
        String startdate = date + " 00:00:00";
        String enddate = date + " 23:59:59";

        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "169");
        s.put("page", "1");
        s.put("pagesize", "100");
        s.put("addtime", startdate);
        s.put("addtime_end", enddate);
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0) {
            for (int i = 1; i <= pages; i++) {
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "169");
                param.put("page", i+"");
                param.put("pagesize", "100");
                param.put("addtime", startdate);
                param.put("addtime_end", enddate);
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray data = res.getJSONArray("data");
                if(data.size()>0){
                    for(int j=0;j<data.size();j++) {
                        JSONObject info = data.getJSONObject(j).getJSONObject("detail");
                        Map pSub = new LinkedHashMap<>();
                        pSub.put("stamp",  System.currentTimeMillis()/1000);
                        pSub.put("datatype", "169");
                        pSub.put("msgid", info.getString("fh_id"));
                        String subPostData = JSON.toJSONString(pSub);
                        String subPostSign = "";
                        try {
                            subPostSign = appSign(id, "view", subPostData);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String subPostUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+subPostSign+"&key="+secretId+"&appid="+id+"&type=view";
                        JSONObject subRes = httpPost(subPostUrl,subPostData);
                        JSONObject detail = subRes.getJSONObject("data").getJSONObject("detail");
                        fhdService.deleteCrmFhdByFhId(detail.getLong("fh_id"));
                        fhdDetailService.deleteCrmFhdDetailByFhNumber(detail.getString("fh_id"));
                        CrmFhd fhd = new CrmFhd();
                        fhd.setFhId(detail.getLong("fh_id"));
                        JSONObject fhCustomer = detail.getJSONObject("fh_customerid");
                        fhd.setFhCustomerid(fhCustomer.getString("id"));
                        fhd.setFhCustomerName(fhCustomer.getString("title"));
                        fhd.setFhDate(detail.getString("fh_date"));
                        JSONObject fhPreside = detail.getJSONObject("fh_preside");
                        fhd.setDeptid(fhPreside.getString("deptid"));
                        fhd.setFhPresideId(fhPreside.getString("id"));
                        fhd.setFhPresideName(fhPreside.getString("title"));
                        fhd.setFhMode(detail.getString("fh_mode"));
                        fhd.setFhNumber(detail.getString("fh_number"));
                        fhd.setFhLxrid(detail.getString("fh_lxrid"));
                        fhd.setFhLinkman(detail.getString("fh_linkman"));
                        fhd.setFhHandset(detail.getString("fh_handset"));
                        fhd.setFhTel(detail.getString("fh_tel"));
                        fhd.setFhTitle(detail.getString("fh_title"));
                        JSONObject fhHtorder = detail.getJSONObject("fh_htorder");
                        fhd.setFhHtorderName(fhHtorder.getString("title"));
                        fhd.setHtNumber(fhHtorder.getString("number"));
                        JSONObject dataUser = detail.getJSONObject("data_userid");
                        fhd.setCreater(dataUser.getString("title"));
                        fhd.setFhRemark(detail.getString("fh_remark"));
                        fhd.setFhState(detail.getString("fh_state"));
                        fhd.setShenhestate(detail.getString("shenhestate"));
                        fhd.setDiyDdl1(detail.getString("diy_ddl1"));
                        fhd.setAddtime(detail.getString("addtime"));
                        fhdService.insertCrmFhd(fhd);
                        JSONArray child_mx = detail.getJSONArray("child_mx");
                        if(child_mx.size()>0){
                            for(int x=0;x<child_mx.size();x++){
                                JSONObject child = child_mx.getJSONObject(x);
                                CrmFhdDetail fhdDetail = new CrmFhdDetail();
                                fhdDetail.setFhNumber(detail.getString("fh_id"));
                                fhdDetail.setProductId(child.getString("产品ID"));
                                fhdDetail.setProductName(child.getString("产品名称"));
                                fhdDetail.setProductType(child.getString("产品型号"));
                                fhdDetail.setProductSpecs(child.getString("产品规格"));
                                fhdDetail.setUnit(child.getString("计件单位"));
                                if(child.getString("数量")!=null&&child.getString("数量").length()>0){
                                    fhdDetail.setNums(new BigDecimal(child.getString("数量")));
                                }
                                if(child.getString("零售价")!=null&&child.getString("零售价").length()>0){
                                    fhdDetail.setRetailPrice(new BigDecimal(child.getString("零售价")));
                                }
                                if(child.getString("单价")!=null&&child.getString("单价").length()>0){
                                    fhdDetail.setUnitPrice(new BigDecimal(child.getString("单价")));
                                }
                                if(child.getString("总价")!=null&&child.getString("总价").length()>0){
                                    fhdDetail.setTotalPrice(new BigDecimal(child.getString("总价")));
                                }
                                fhdDetail.setRemarks(child.getString("明细备注"));
                                fhdDetailService.insertCrmFhdDetail(fhdDetail);
                            }
                        }
                    }
                }
            }
        }
        return Integer.parseInt(result.get("totalCount").toString());
    }

    @Override
    public void getYgxxData() {
        String id = "89711";
        String secretId = "1hfDGyy85VCT";
        String type = "list_simple";
        String postData = "";
        Map s = new LinkedHashMap<>();
        s.put("stamp",  System.currentTimeMillis()/1000);
        s.put("datatype", "167");
        s.put("page", "1");
        s.put("pagesize", "100");
        postData = JSON.toJSONString(s);
        String appSign = "";
        try {
            appSign=appSign(id, type, postData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payUrl="https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+appSign+"&key="+secretId+"&appid="+id+"&type="+type;
        JSONObject result = httpPost(payUrl,postData);
        int pages = 0;
        if(Integer.parseInt(result.get("totalCount").toString())%100>0){
            pages = (int)Math.floor((double) Integer.parseInt(result.get("totalCount").toString())/100)+1;
        }else{
            pages = Integer.parseInt(result.get("totalCount").toString())/100;
        }
        if (pages>0){
            ygxxService.clearCrmYgxx();
            for(int i=1;i<=pages;i++){
                Map param = new LinkedHashMap<>();
                param.put("stamp",  System.currentTimeMillis()/1000);
                param.put("datatype", "167");
                param.put("page", i+"");
                param.put("pagesize", "100");
                String postParam = JSON.toJSONString(param);
                String postSign = "";
                try {
                    postSign = appSign(id, type, postParam);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String postUrl = "https://crmapi.jzsoft.cn/apigetdata.aspx?sign="+postSign+"&key="+secretId+"&appid="+id+"&type="+type;
                JSONObject res = httpPost(postUrl,postParam);
                JSONArray staff = res.getJSONArray("data");
                if(staff.size()>0){
                    for (int j=0;j<staff.size();j++){
                        JSONObject info = staff.getJSONObject(j).getJSONObject("detail");
                        CrmYgxx ygxx = new CrmYgxx();
                        ygxx.setEmpid(info.getString("empid"));
                        ygxx.setEmpName(info.getString("emp_name"));
                        ygxx.setEmpNo(info.getString("emp_no"));
                        JSONObject deptInfo = info.getJSONObject("deptid");
                        ygxx.setDeptid(deptInfo.getString("id"));
                        ygxx.setDeptName(deptInfo.getString("title"));
                        ygxx.setDutyid(info.getString("dutyid"));
                        ygxx.setEmpZaizhi(info.getString("emp_zaizhi"));
                        ygxxService.insertCrmYgxx(ygxx);
                    }
                }
            }
        }
    }

    public static JSONObject httpPost(String url, String strParam) {
        // post请求返回结果
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonResult = null;
        HttpPost httpPost = new HttpPost(url);
        // 设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT).setConnectTimeout(TIMEOUT).build();
        httpPost.setConfig(requestConfig);
        try {
            if (null != strParam) {
                // 解决中文乱码问题
                StringEntity entity = new StringEntity(strParam, "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/x-www-form-urlencoded");
//                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            CloseableHttpResponse result = httpClient.execute(httpPost);
            // 请求发送成功，并得到响应
            if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String str = "";
                try {
                    // 读取服务器返回过来的json字符串数据
                    str = EntityUtils.toString(result.getEntity(), "utf-8");
                    // 把json字符串转换成json对象
                    jsonResult = JSONObject.parseObject(str);
                } catch (Exception e) {
                    logger.error("post请求提交失败:" + url, e);
                }
            }else {
                String getError = EntityUtils.toString(result.getEntity(), "utf-8");
            }
        } catch (IOException e) {
            logger.error("post请求提交失败:" + url, e);
        } finally {
            httpPost.releaseConnection();
        }
        return jsonResult;
    }

    public static String appSign(String id, String type, String postData) throws Exception {
        String plainText = id+type+postData;
        byte[] hmacDigest = hmacsha1(plainText, secretKey);
        String a = URLEncoder.encode(Base64Encode(hmacDigest).toUpperCase(),ENCODING);
        return a;
    }

    public static String Base64Encode(byte[] binaryData){
        String encodedstr = Base64.getEncoder().encodeToString(binaryData);
        return encodedstr;
    }


    public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception{
        Mac mac = Mac.getInstance(MAC_NAME);
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(ENCODING), MAC_NAME);
        mac.init(secretKey);
        byte[] HmacSha1Digest = mac.doFinal(binaryData);
        return HmacSha1Digest;
    }

    public static byte[] hmacsha1(String plainText, String key) throws Exception{
        return HmacSha1(plainText.getBytes(ENCODING), key);
    }

    public String getNearWeekDate(int num){
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        // 设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_YEAR, num);
        String pastDay = sdf.format(calendar.getTime());
        return pastDay;
    }

    public String getCurrentDate() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        return formattedDate;
    }
}
